package code.rocky.chapter08;

public class Suggest120 {
    public static void main(String[] args) throws InterruptedException {

        // demo01();
        // demo02();
        demo03();

    }

    private static void demo03() {
        Thread t1 = new Thread() {
            public void run() {
                while (true) {
                    System.out.println("Running......");
                }
            }
        };
        t1.start();
        t1.interrupt();
    }

    private static void demo02() {
        MultiThread t = new MultiThread();
        Thread t1 = new Thread(t);
        t1.start();
        for (int i = 0; i < 5; i++) {
            new Thread(t).start();
        }
        t1.stop();
    }

    private static void demo01() throws InterruptedException {
        // 子线程
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // 异常处理
                }
                System.out.println("此处代码不会执行");
            }
        };
        thread.start();
        Thread.sleep(100);
        thread.stop();
    }
}

class SafeStopThread extends Thread {

    private volatile boolean stop = false;

    @Override
    public void run() {
        while (stop) {

        }
    }
    public void terminate() {
        stop = true;
    }
}

class MultiThread implements Runnable {

    int a = 0;

    @Override
    public void run() {
        synchronized ("") {
            a++;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            a--;
            String tn = Thread.currentThread().getName();
            System.out.println(tn + ":a =" + a);
        }
    }
}
